# -*- coding:utf-8 -*-
"""
@File       :   handle_yaml.py
@Author     :   LINING
@Time       :   2023/10/31 10:41
@Description :   yaml配置文件读取
"""
import yaml
import os

# 项目根目录文件路径
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# config文件路径
CONFIG_PATH = os.path.join(BASE_PATH, 'Config', 'config.yaml')
# 日志文件
LOG_PATH = os.path.join(BASE_PATH, 'Log')
# 测试数据
DATA_PATH = os.path.join(BASE_PATH, 'Data')
# 测试用例
TESTCASE_PATH = os.path.join(BASE_PATH, 'TestCase')
# 截屏图片
SCREENSHOT_PATH = os.path.join(BASE_PATH, 'Screenshots')
# 测试报告
REPORT_PATH = os.path.join(BASE_PATH, 'Report')
# 测试html
HTML_PATH = os.path.join(BASE_PATH, 'Report', 'html')
# 测试文件
Test_File = os.path.join(BASE_PATH, 'TestFile')


class YamlReader:
    def __init__(self, path=CONFIG_PATH):
        """
        读取配置信息
        :param config_path: 配置文件路径
        """
        if os.path.exists(path):
            self.config_path = path
            self._data = self.read_config_info(path)
        else:
            raise FileNotFoundError('配置文件路径%s不存在！！！' % path)

    def read_config_info(self, config_path):
        """
        读取配置文件的信息
        :param config_path: 指定配置文件的路径
        :return: 读取到配置文件的信息，返回dic字典格式
        """
        try:
            with open(config_path, 'r', encoding='utf-8') as f:
                return yaml.load(f.read(), Loader=yaml.Loader)
        except Exception as e:
            print("file_path:" + config_path + ",yaml文件格式错误，请检查文件格式,错误信息:%s" % str(e))

    def get_value(self, keys):
        """
        获取配置值
        :param keys: 可输入多个子级key值
        :return: 返回配置值
        """
        return self.get_dict_value(keys, self._data)

    def get_dict_value(self, keys, dict_data):
        """
        获取配置值
        :param keys: 可输入多个子级key值,列表
        :param dict_data: 读取的配置文件值
        :return: 返回配置值
        """
        dic = dict_data
        value = None
        if len(keys) > 0:
            for key in keys:
                if dic.__contains__(key):
                    value = dic.get(key)
                    if isinstance(value, dict):
                        dic = dict(value)
                else:
                    return None
        else:
            value = dic
        return value

    def write_file(self, data, path):
        """
        写文件
        :return:
        """
        with open(path, mode='w', encoding='utf-8') as file:
            yaml.dump(data, file, encoding='utf-8', allow_unicode=True)


config_data = YamlReader()
# 执行环境
execute = config_data.get_value(['execute'])
# 管理端账号密码
account_admin = config_data.get_value(['account', f'{execute}_admin'])
# 用户端账号密码
account_user = config_data.get_value(['account', f'{execute}_user'])
# saas域名
domain_saas= config_data.get_value(['domain', f'{execute}_saas'])
# saas账号密码
account_saas = config_data.get_value(['account', f'{execute}_saas'])
# 执行的系统
system = config_data.get_value(['execute_system']).lower()
# 保存token的文件
token_file = os.path.join(BASE_PATH, 'Config', f'{system}_token')
# 域名
domain = config_data.get_value(['domain', f'{execute}_{system}'])


if __name__ == '__main__':
    print(token_file)
